<title>产品分类管理</title>

<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">主页</a>
    <a><cite>产品分类管理</cite></a>
    <a><cite>产品分类列表</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="layadmin-useradmin-formlist">
      <div class="layui-form-item">
        <!-- 搜索区域 -->


      </div>
    </div>

    <div class="layui-card-body">
      <div style="padding-bottom: 10px;">
        <button class="layui-btn layuiadmin-btn-admin" data-type="add" id="add">添加</button>
      </div>

      <table id="table-instance" lay-filter="table-instance"></table>

      <script type="text/html" id="table-useradmin-admin">
       <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-image layui-image-edit"></i>编辑</a>
       <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-image "></i>删除</a>
      </script>
    </div>
  </div>
</div>

<script>
  layui.use('useradmin', layui.factory('useradmin')).use(['admin', 'useradmin', 'table', 'laydate', 'layedit', 'upload'], function () {
    var $ = layui.$
      , admin = layui.admin
      , view = layui.view
      , setter = layui.setter
      , table = layui.table
      , form = layui.form
      , layedit = layui.layedit
      , laydate = layui.laydate
      , upload = layui.upload



    form.render(null, 'layadmin-useradmin-formlist');
    //监听搜索
    form.on('submit(LAY-user-back-search)', function (data) {
      var field = data.field;
      //执行重载
      table.reload('table-instance', {
        where: field
      });
    });
    //事件


    var active = {
      add: function () {
        admin.popup({
          title: '添加产品分类'
          , area: ["500px", "450px"]
          , id: 'LAY-popup-useradmin-add'
          , success: function (layero, index) {
            view(this.id).render('producttype/form').done(function () {
              form.render(null, 'layuiadmin-form-admin');
              //监听提交

              form.on('submit(LAY-user-back-submit)', function (data) {
                var field = data.field; //获取提交的字段


                editcardHandle(Object.assign(data, field));
                layui.table.reload('LAY-user-front-submit'); //重载表格
                layer.close(index); //执行关闭 
              });
            });
          }
        });
      }
    };
    table.render({
      elem: "#table-instance",
      url: setter.request.baseUrl + "productType/list",
      method: "get",
      headers: { token: layui.data("layuiAdmin").token },
      response: {
        statusName: "code",
        statusCode: 0,
        msgName: "msg",
        countName: "totalCount",
        dataName: "productType",
      },
      cols: [
        [
          { field: "name", title: "分类名" },
          { fixed: "right", title: "操作", toolbar: "#table-useradmin-admin" },
        ],
      ],
      page: true,
    });

    //删除模块
    table.on("tool(table-instance)", function (obj) {
      var data = obj.data;
      if (obj.event === "del") {
        layer.confirm("真的删除么", function (index) {
          delcardHandle({
            id: data.id,
          });
          obj.del();
          layer.close(index);
        });
      } else if (obj.event === "edit") {
        admin.popup({
          title: "编辑产品分类",
          area: ["500px", "450px"],
          success: function (layero, index) {
            view(this.id)
              .render("producttype/form", data)
              .done(function () {
                let _data = data;


                form.render(null, "layuiadmin-form-admin");

                form.on('submit(LAY-user-back-submit)', function (data) {
                  var field = data.field;


                  editcardHandle(Object.assign(_data, field));
                  layer.close(index);
                });
              });
          },
        });
      }
    });

    //修改编辑
    function editcardHandle(params) {
      admin.req({
        url: setter.request.baseUrl + "productType/save",
        data: JSON.stringify(params),
        type: "post",
        dataType: "json"
        , contentType: "application/json",
        done: function (res) {
          layui.table.reload("table-instance");
        },
      });
    }

    //删除
    function delcardHandle(params) {
      admin.req({
        url: setter.request.baseUrl + "productType/delete",
        data: {
          ...params,
        },
        type: "get",
        done: function (res) {
          layer.msg(res.msg);
        },
      });
    }

    $('.layui-btn.layuiadmin-btn-admin').on('click', function () {
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });

    // 预览图片
    $(document).on("click", ".table-check-img", function (e) {
      let json = {
        title: "查看卡图片",
        id: 0,
        start: 0,
        data: [
          {
            alt: "",
            pid: 1,
            src: $(e.target).data("img"), //原图地址
            thumb: "",
          },
        ],
      };
      if (json.data[0].src == null) {
        layer.msg("暂无图片", {
          offset: "15px",
          icon: "2",
          time: 1000
        })
      } else {
        var result = json.data[0].src.split(",");
        var j = new Array;
        for (var i = 0; i < result.length; i++) {
          console.log(result[i])
          let json = {
            "src": result[i]
          }
          j[i] = json;
        }
        layer.photos({
          photos: { "data": j },
          anim: 1,
        });
      }

    });

  });

</script>

<script type="text/html" id="Image">
  <button type="button" class="layui-btn layui-btn-xs table-check-img" data-img="{{d.image}}">查看图片</button>
</script>